package com.thlws.mongo.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.thlws.mongo.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author HanleyTang 2019-06-21
 */
@Service
public class DemoService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    static List<String> phonePrefix = new ArrayList<>();

    @Autowired
    private MongoTemplate mongoTemplate;

    private final String USER_COLLECTION = "user";


    static {

        final String[] arr = new String[]{"13", "14", "15", "17", "18"};
        for (String p : arr) {
            for (int i = 0; i < 10; i++) {
                phonePrefix.add(p + i);
            }
        }
    }


    public User insert(){

        User user = mongoTemplate.insert(genRandomUser(), USER_COLLECTION);
        logger.info("insert result:{}",JSONUtil.toJsonPrettyStr(user));
        return user;
    }


    public long update(String userName,String phone){

        Query query = new Query();
        query.addCriteria(Criteria.where("username").is(userName));
        query.addCriteria(Criteria.where("phone").is(phone));

        Update update = new Update();
        update.set("age", 32);
        update.set("email", "hanley@thlws.com");
        update.set("tall", 1.72);
        update.set("weight", 55);
        update.set("address", "SH");
        update.set("userName", "han");
        update.set("phone", "18650002030");

        UpdateResult result = mongoTemplate.updateMulti(query, update, USER_COLLECTION);

        return result.getModifiedCount();

    }



    public long insertOrUpdate(String userName,String phone){

        Query query = new Query();
        query.addCriteria(Criteria.where("username").is(userName));
        query.addCriteria(Criteria.where("phone").is(phone));

        User user = genRandomUser();

        Update update = new Update();
        update.set("age", user.getAge());
        update.set("email", user.getEmail());
        update.set("tall", user.getTall());
        update.set("weight", user.getWeight());
        update.set("address", user.getAddress());
        update.set("userName", userName);
        update.set("phone", phone);

        UpdateResult result = mongoTemplate.upsert(query, update, USER_COLLECTION);

        return result.getModifiedCount();

    }


    public long remove(String userName,String phone){

        Query query = new Query();
        query.addCriteria(Criteria.where("username").is(userName));
        query.addCriteria(Criteria.where("phone").is(phone));

        DeleteResult result = mongoTemplate.remove(query, USER_COLLECTION);

        return result.getDeletedCount();

    }





    private User genRandomUser(){

        String randomPhone =RandomUtil.randomEle(phonePrefix)+RandomUtil.randomNumbers(8);

        User user = new User();
        user.setAddress(RandomUtil.randomString(5));
        user.setAge(RandomUtil.randomInt(10, 60));
        user.setEmail(RandomUtil.randomString(5)+"@demo.com");
        user.setPhone(randomPhone);
        user.setTall(RandomUtil.randomDouble(1.5D,2.0D));

        user.setUsername(RandomUtil.randomString(3));
        user.setWeight(RandomUtil.randomDouble(10D,100D));

        return user;
    }



}
